package com.example.demo.controller;


import com.example.demo.entity.QRCode;
import com.example.demo.entity.SaveCC;
import com.example.demo.service.QRCodeReceiverService;
import com.example.demo.service.QRCodeService;
import com.example.demo.vo.DataView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;

@RestController
@RequestMapping(value = "/qrcode")
public class QRCodeReceiverOrSendController {

    @Value("${qrCode.voices}")
    private String voicePath;

    @Value("${qrCode.videos}")
    private String videoPath;

    @Value("${qrCode.images}")
    private String imagesPath;

    @Autowired
    private QRCodeReceiverService qrCodeReceiverService;

    @Autowired
    private QRCodeService qrCodeService;


    @RequestMapping(value = "/receiveImages")
    public DataView receiveImage(@RequestParam(value = "images")MultipartFile[] images, HttpServletRequest request) throws IOException {
        return qrCodeReceiverService.receiveImage(images,request);
    }

    @RequestMapping(value = "/receiveVideos")
    public DataView receiveVideo(@RequestParam(value = "videos")MultipartFile[] videos,HttpServletRequest request) throws IOException {
        return qrCodeReceiverService.receiveVideo(videos,request);
    }

    @RequestMapping(value = "/receiveVoice")
    public DataView receiveVoice(@RequestParam(value = "voice") MultipartFile voice,HttpServletRequest request) throws IOException {
        return qrCodeReceiverService.recevieVoice(voice,request);
    }

    @RequestMapping(value = "/save")
    public DataView save(@RequestBody SaveCC saveCC, HttpServletRequest request)
    {
        DataView dataView = new DataView();
        String qrcode = request.getHeader("qrcode");
        if (qrcode == null) {
            dataView.setCode(300);
            dataView.setMsg("二维码不存在");
            return dataView;
        }

        QRCode code = qrCodeService.query().eq("qr_code_id", qrcode).one();

        if(code == null)
        {
            dataView.setCode(300);
            dataView.setMsg("二维码不存在");
            return dataView;
        }
        code.setIsUse(1);
        qrCodeService.updateById(code);

        saveCC.setQrCodeId(qrcode);
        qrCodeReceiverService.save(saveCC);
        dataView.setData(saveCC);
        return dataView;
    }

    @RequestMapping(value = "/send")
    public DataView send(HttpServletRequest request)
    {

        return qrCodeReceiverService.send(request);
    }

}
